<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    >
      <template v-if="meta.indexOf('AdvExportV') > -1" #btns>
        <el-button :loading="exportLoading" type="primary" @click="batchexport">{{ $t('message.daochu') }}</el-button>
      </template>
    </QueryForm>
    <!-- <div class="warnningTip">预收款只记录2024-08-05之后的数据，以往数据不记录。</div> -->
    <div v-if="tjList && tjList.length > 0" class="total_wrap">
      <div class="first item">
        <div class="key">{{ $t('message.shanxuan') }}</div>
        <div class="key">{{ $t('message.heji') }}</div>
      </div>
      <div class="item_list">
        <div v-for="(item, i) in tjList" :key="i" class="item">
          <div class="key">{{ item.payMethod }}</div>
          <div class="value">
            <!-- <span>{{ $t('message.rmbfh') }}</span> -->
            <span class="price">{{ item.money }}</span>
          </div>
        </div>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" :label="$t('message.memberInfo')" prop="memberInfo" min-width="180" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.ysklx')" prop="typeName" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.typeName === '01'?$t('message.kc'):scope.row.typeName === '02'?$t('message.tc'):scope.row.typeName }}
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('message.yskcp')" prop="productName" min-width="140" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.yskddbh')" prop="orderId" min-width="200" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.ysze')" prop="paymentAll" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.qqje')" prop="confMoney" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.qxddywh')" prop="targetId" min-width="220" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.qqywlx')" prop="confTypeName" min-width="130" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.qqsj')" prop="createTime" min-width="160" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.qqsm')" prop="remarks" min-width="140" show-overflow-tooltip />
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { advcPageList, advcExport } from '@/api'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
import dayjs from 'dayjs'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      exportLoading: false,
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      meta: [],
      total: 0,
      filters: {},
      listLoading: false,
      tjList: []
    }
  },
  created() {
    const meta = this.$route.meta.buttons || []
    this.meta = meta
    let time = []
    const start = new Date()
    const end = new Date()
    start.setTime(end.getTime() - 3600 * 1000 * 24 * 29)
    time = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
    this.$set(this.filters, 'time', time)
    this.getList(1)
  },
  methods: {
    nameWithPayMethod(payM) {
      const temp = this.allPayMethedList.find((item) => item.value === payM)
      return temp ? temp.name : '-'
    },
    clearList() {
      this.filters = {}
      this.getList(1)
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (filters.time && filters.time.length > 0) {
        filters.startTime = filters.time[0]
        filters.endTime = filters.time[1]
      } else {
        filters.startTime = null
        filters.endTime = null
      }
      filters.venueId = sessionStorage.getItem('venueId')
      pagination.firstQueryTime = parseTime(new Date())
      this.listLoading = true
      advcPageList({ pagination, param: { ...filters }}).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
          const tjList = res.data || []
          this.tjList = tjList
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    batchexport() {
      const { filters } = this
      if (filters.time && filters.time.length > 0) {
        filters.startTime = filters.time[0]
        filters.endTime = filters.time[1]
      } else {
        filters.startTime = null
        filters.endTime = null
      }
      filters.venueId = sessionStorage.getItem('venueId')
      this.exportLoading = true
      advcExport({ param: { ...filters }}).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>

